<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="hRole(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageForms.page"
                :page-sizes="[2, 3, 4, 8]"
                :page-size="pageForms.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit?'编辑角色':'新增角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hclose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 给角色分配权限 -->
      <el-dialog title="分配权限" :visible.sync="showPermission">
        <assignPermission v-if="showPermission" :role-id="roleId" @close="showPermission=false" @success="showPermission=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import assignPermission from './assignPermission.vue'
import { getRoles, addRoles, editRoles, delRoles } from '@/api/setting'
export default {
  name: 'SettingPage',
  components: { assignPermission },
  data() {
    return {
      showPermission: false,
      list: [],
      pageForms: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      showDialog: false,
      isEdit: false,
      // 角色id
      roleId: '',
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.upDateRoles()
  },
  methods: {
    // 点击分配权限
    hRole(id) {
      this.roleId = id
      this.showPermission = true
    },
    // 关闭对话框
    hclose() {
      // 重置表单
      this.$refs.roleForm.resetFields()
      // 清空表单
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 提交按钮
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hEdit(row) {
      // 将传递过来的角色详情数据传递过来
      // 使用浅拷贝解决
      this.roleForm = { ...row }
      this.showDialog = true
      this.isEdit = true
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    hDel(id) {
      this.$confirm('确认删除此角色?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delRoles(id)
        console.log(res)
        if (this.list.length === 1) {
          this.pageForms.page--
          if (this.pageForms.page <= 0) {
            this.pageForms.page = 1
          }
        }
        if (res.code === 10000) {
          this.upDateRoles()
          this.$message.success('删除数据成功')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑角色
    async doEdit() {
      try {
        console.log(this.roleForm)
        await editRoles(this.roleForm)
        this.upDateRoles()
        // 关闭弹层
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 添加角色
    async doAdd() {
      try {
        await addRoles(this.roleForm)
        this.upDateRoles()
        const maxPage = Math.ceil((this.total + 1) / this.pageForms.pagesize)
        this.pageForms.page = maxPage
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    async upDateRoles() {
      const res = await getRoles(this.pageForms)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 切换每页的条数
    handleSizeChange(val) {
      this.pageForms.page = 1
      this.pageForms.pagesize = val
      this.upDateRoles()
    },
    // 切换页码
    handleCurrentChange(val) {
      this.pageForms.page = val
      this.upDateRoles()
    }
  }
}
</script>

<style>
</style>
